package com.mz.ozy.utils;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

/**
 * Created by administrator on 2019/4/2.
 */
public class CalendarUtils {

    public static String getFormatDate(String date) {
        Date curDate = null;
        try {
            curDate = CalendarUtils.string2Date(date);
        } catch (ParseException e) {
        }
        if (curDate == null) {
            return null;
        } else {
            return CalendarUtils.date2String(curDate);
        }
    }

    public static Date string2Date(String date) throws ParseException {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//注意月份是MM
        return simpleDateFormat.parse(date);
    }

    public static String date2String(Date date) {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy年MM月dd日");//注意月份是MM
        return simpleDateFormat.format(date);
    }

    public static String getWeekOfDate(Date date) {
        String[] weekDays = {"周日", "周一", "周二", "周三", "周四", "周五", "周六"};
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        int w = cal.get(Calendar.DAY_OF_WEEK) - 1;
        if (w < 0)
            w = 0;
        return weekDays[w];
    }


    /**
     * 获取今日月份
     * 月份是从0开始，所以要自觉+1
     *
     * @return
     */
    public static String getTodayMonth() {
        return Calendar.getInstance().get(Calendar.MONTH) + 1 + "";
    }

    /**
     * 获取今日日期
     *
     * @return
     */
    public static String getTodayDay() {
        return Calendar.getInstance().get(Calendar.DATE) + "";
    }


    /**
     * 获取本周的日期  YYYY-MM-dd
     *
     * @return
     */
    public static String[] getCurrentWeekDay() {
        String[] weekDays = new String[7];
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        Calendar cal = Calendar.getInstance();
        cal.setTime(new Date());
        // 判断要计算的日期是否是周日，如果是则减一天计算周六的，否则会出问题，计算到下一周去了  
        int dayWeek = cal.get(Calendar.DAY_OF_WEEK);// 获得当前日期是一个星期的第几天  
        if (1 == dayWeek) {
            cal.add(Calendar.DAY_OF_MONTH, -1);
        }
        // 设置一个星期的第一天，按中国的习惯一个星期的第一天是星期一  
        cal.setFirstDayOfWeek(Calendar.MONDAY);
        // 获得当前日期是一个星期的第几天  
        int day = cal.get(Calendar.DAY_OF_WEEK);
        // 根据日历的规则，给当前日期减去星期几与一个星期第一天的差值  
        cal.add(Calendar.DATE, cal.getFirstDayOfWeek() - day);
        String monday = sdf.format(cal.getTime());  //周一
        weekDays[0] = monday;
        for (int i = 1; i < 7; i++) {
            cal.add(Calendar.DATE, 1);
            weekDays[i] = sdf.format(cal.getTime());
        }
        return weekDays;
    }


    /**
     *  
     *  根据当前日期获得所在周的日期区间（周一和周日日期） 
     */
    public static String getTimeInterval(Date date) {
        SimpleDateFormat sdf = new SimpleDateFormat("MM.dd");
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        // 判断要计算的日期是否是周日，如果是则减一天计算周六的，否则会出问题，计算到下一周去了  
        int dayWeek = cal.get(Calendar.DAY_OF_WEEK);// 获得当前日期是一个星期的第几天  
        if (1 == dayWeek) {
            cal.add(Calendar.DAY_OF_MONTH, -1);
        }
        // 设置一个星期的第一天，按中国的习惯一个星期的第一天是星期一  
        cal.setFirstDayOfWeek(Calendar.MONDAY);
        // 获得当前日期是一个星期的第几天  
        int day = cal.get(Calendar.DAY_OF_WEEK);
        // 根据日历的规则，给当前日期减去星期几与一个星期第一天的差值  
        cal.add(Calendar.DATE, cal.getFirstDayOfWeek() - day);
        String imptimeBegin = sdf.format(cal.getTime());
        System.out.println("所在周星期一的日期：");
        System.out.println(imptimeBegin);
        cal.add(Calendar.DATE, 6);
        String imptimeEnd = sdf.format(cal.getTime());
        System.out.println("所在周星期日的日期：");
        System.out.println(imptimeEnd);
        return imptimeBegin + "," + imptimeEnd;
    }

    /**
     * 显示几天钱几周前几月前几年前
     *
     * @param d
     * @return
     */
    public static String getFriendlytime(Date d) {
        long delta = (new Date().getTime() - d.getTime()) / 1000;
        if (delta <= 0) return d.toLocaleString();
        if (delta / (60 * 60 * 24 * 365) > 0) return delta / (60 * 60 * 24 * 365) + "年前";
        if (delta / (60 * 60 * 24 * 30) > 0) return delta / (60 * 60 * 24 * 30) + "个月前";
        if (delta / (60 * 60 * 24 * 7) > 0) return delta / (60 * 60 * 24 * 7) + "周前";
        if (delta / (60 * 60 * 24) > 0) return delta / (60 * 60 * 24) + "天前";
        if (delta / (60 * 60) > 0) return delta / (60 * 60) + "小时前";
        if (delta / (60) > 0) return delta / (60) + "分钟前";
        return "刚刚";
    }
}
